package com.donger.business.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.auth.utils.SecurityUtils;
import com.donger.business.message.entity.MessageSendLog;
import com.donger.business.message.service.MessageSendLogService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.query.utils.QueryGenerator;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author 7326
 */
@RestController
@RequestMapping("/message/sendlog")
@AllArgsConstructor
@Api(tags = "消息发送日志开发API")
public class MessageSendLogController {

    private final MessageSendLogService messageSendLogService;


    /**
     * 查询 列表
     * @param page
     * @param messageSendLog
     * @return
     */
    @GetMapping("/page")
    public Result page(Page page, MessageSendLog messageSendLog){
        QueryWrapper<MessageSendLog> messageSendLogQueryWrapper = QueryGenerator.<MessageSendLog>initQueryWrapper(messageSendLog);
        messageSendLogService.page(page,messageSendLogQueryWrapper);
        return Res.ok(page);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
        MessageSendLog messageSendLog = messageSendLogService.getById(id);
        return Res.ok(messageSendLog);
    }


    /**
     * 保存
     * @param messageSendLog
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody MessageSendLog messageSendLog){
        messageSendLogService.save(messageSendLog);
        return Res.ok();
    }

    /**
     * 修改
     * @param messageSendLog
     * @return
     */
    @PutMapping("/updata")
    public Result update(@RequestBody MessageSendLog messageSendLog){
        messageSendLogService.updateById(messageSendLog);
        return Res.ok();
    }


    /**
     * 删除
     * @param id 删除的数据id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id){
        messageSendLogService.removeById(id);
        return Res.ok();
    }

    /**
     * 我接受的消息
     * @param page
     * @param messageSendLog
     * @return
     */
    @GetMapping("/pageByUser")
    public Result pageByUser(Page page, MessageSendLog messageSendLog){
        Long id = SecurityUtils.getUser().getId();
        QueryWrapper<MessageSendLog> messageSendLogQueryWrapper = QueryGenerator.<MessageSendLog>initQueryWrapper(messageSendLog);
        messageSendLogQueryWrapper.<MessageSendLog>lambda().eq(MessageSendLog::getRecipientId,id);
        messageSendLogService.page(page,messageSendLogQueryWrapper);
        return Res.ok(page);
    }
}
